package org.xwalk.core;


import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;

/**
 * A callback used by the host application to notify
 * the current page that its custom view has been dismissed.
 */

public class CustomViewCallbackHandler   implements CustomViewCallback  {

    private ArrayList<Object> constructorTypes;
    private ArrayList<Object> constructorParams;
    private ReflectMethod postWrapperMethod;


    private XWalkCoreWrapper coreWrapper;
    private Object bridge;

    protected Object getBridge() {
        return bridge;
    }

    public CustomViewCallbackHandler(Object bridge) {
        this.bridge = bridge;
        reflectionInit();
    }

    /**
     * Invoked when the host application dismisses the
     * custom view.
     * @since 7.0
     */
    public void onCustomViewHidden() {
        try {
            onCustomViewHiddenMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        
    }

    private ReflectMethod onCustomViewHiddenMethod = new ReflectMethod(null, "onCustomViewHidden");


    void reflectionInit() {
        XWalkCoreWrapper.initEmbeddedMode();

        coreWrapper = XWalkCoreWrapper.getInstance();
        if (coreWrapper == null) {
            XWalkCoreWrapper.reserveReflectObject(this);
            return;
        }

        onCustomViewHiddenMethod.init(bridge, null,
                "onCustomViewHiddenSuper");
    }

}
